#include "ActionDessinGrabToLauch.h"
#include "math.h"
#include "Dessin.h"


ActionDessinGrabToLauch::ActionDessinGrabToLauch() : ActionDessinGrab() {
    moving =false;
    direction_x = 0;
    direction_y = 0;
    temps_ecoule_grab = 0;
}

void ActionDessinGrabToLauch::OnMousePressed(Dessin* d,wxMouseEvent &event){
    if(!selected){
         point_depart_x=d->GetPosition().x;
         point_depart_y=d->GetPosition().y;
    }
    if(!moving) ActionDessinGrab::OnMousePressed(d,event);


}

void ActionDessinGrabToLauch::OnMotion(Dessin* d,wxMouseEvent &event){
    ActionDessinGrab::OnMotion(d,event);
}

void ActionDessinGrabToLauch::OnRelease(Dessin* d,wxMouseEvent &event){
    ActionDessinGrab::OnRelease(d,event);   
    if(!moving){
        direction_x = round((d->GetPosition().x - point_depart_x)/temps_ecoule_grab);
        direction_y = round((d->GetPosition().y  - point_depart_y)/temps_ecoule_grab);
        moving = true;
    }
}

void ActionDessinGrabToLauch::OnTimer(Dessin* d,wxTimerEvent& event){
    if(moving){        
        deplacer(d,direction_x,direction_y);
        if(ActionDessinGrab::anyEdgeReached(d))   correctDimension(d);
        d->Refresh () ;
    }
    if(selected) temps_ecoule_grab++;

}

void ActionDessinGrabToLauch::correctDimension(Dessin* d){

    if(ActionDessinGrab::nEdgeReached(d)){
        direction_y = -direction_y;
    }
    if(ActionDessinGrab::wEdgeReached(d)){
        direction_x = -direction_x;
    }
    if(ActionDessinGrab::eEdgeReached(d)){
        direction_x = -direction_x;
    }
    if(ActionDessinGrab::sEdgeReached(d)){
        direction_y = -direction_y;
    }
    ActionDessinGrab::correctDimension(d);
}

void ActionDessinGrabToLauch::deplacer(Dessin* d,long entree_mouv_x,long entre_mouv_y){
        long x1,y1,x2,y2;
        x1 = d->GetPosition().x;
        y1 = d->GetPosition().y;
        x2 = x1+(d->GetSize().GetWidth());
        y2 = y1+(d->GetSize().GetHeight());


        x1+=entree_mouv_x;
        y1+=entre_mouv_y;
        x2+=entree_mouv_x;
        y2+=entre_mouv_y;

        d->SetSize(x1,y1,x2-x1,y2-y1);

}


